import { creepDefaultMemory } from "@/app/spawn/constant"
import { UPGRADE_ENERGY_SEND_LIMIT } from "@/app/structures/link"
import { CreepRole, RoleDatas } from "@/role/types/role"
import CreepController from "../controller"


class RoleCenterManager {

    static sleep(creep: Creep) {
        creep.say('Zzz')
        const storage = creep.room[STRUCTURE_STORAGE]
        if (storage) {
            if (creep.store[RESOURCE_ENERGY] > 0) {
                creep.goTo(storage.pos)
                const status = creep.transfer(storage, RESOURCE_ENERGY)
                if (status == OK) {
                    creep.memory.working = false
                }
            }
            if (creep.store[RESOURCE_GHODIUM_HYDRIDE] > 0) {
                creep.goTo(storage.pos)
                const status = creep.transfer(storage, RESOURCE_GHODIUM_HYDRIDE)
                if (status == OK) {
                    creep.memory.working = false
                }
            }
            if (creep.store[RESOURCE_GHODIUM_ACID] > 0) {
                creep.goTo(storage.pos)
                const status = creep.transfer(storage, RESOURCE_GHODIUM_ACID)
                if (status == OK) {
                    creep.memory.working = false
                }
            }
        }
        const terminal = creep.room[STRUCTURE_TERMINAL]
        if (terminal) {
            if (!creep.pos.isNearTo(terminal.pos)) {
                creep.goTo(terminal.pos)
                creep.memory.isStanBy = true
            }
        }
    }

    /**
     * 快死时的后事处理
     * 将资源存放在对应的地方
     * 存完了就自杀
     *
     * @param creep manager
     * @param sourceId 能量存放处
     */
    static deathPrepare(creep: Creep): false {
        if (creep.store.getUsedCapacity() > 0) {
            for (const resourceType in creep.store) {
                let target: StructureWithStore
                // 不是能量就放到 terminal 里
                if (resourceType != RESOURCE_ENERGY && resourceType != RESOURCE_POWER && creep.room.terminal) {
                    target = creep.room.terminal
                }
                // 否则就放到 storage 或者玩家指定的地方
                else target = creep.room.storage
                // 刚开新房的时候可能会没有存放的目标
                if (!target) return false

                // 转移资源
                creep.goTo(target.pos)
                creep.transfer(target, <ResourceConstant>resourceType)

                return false
            }
        }
        else creep.suicide()

        return false
    }

    /**
     *
     * @param creep
     * @returns
     */
    static loop(creep: Creep) {

        const memoryData: RoleDatas[CreepRole.CenterManager] = creep.memory.data as RoleDatas[CreepRole.CenterManager]

        if (!memoryData) {
            creep.log('memory error')
            return
        }

        if (!creep.memory.isStanBy) {
            const terminal = creep.room[STRUCTURE_TERMINAL]
            if (terminal) {
                creep.goTo(terminal.pos)
                if (creep.pos.isNearTo(terminal.pos)) {
                    creep.memory.isStanBy = true
                }
                return
            }
        }

        if (creep.ticksToLive < 30) {
            creep.say('->deathPre')
            return RoleCenterManager.deathPrepare(creep)
        }

        if (!creep.memory.working && creep.store.getFreeCapacity() > 0) {

            const storage = creep.room[STRUCTURE_STORAGE]
            const terminalObj = creep.room[STRUCTURE_TERMINAL]

            // 如果房间不是在用终端发送资源，则掏空终端
            if (!creep.room.memory.isTerminalSending && terminalObj && terminalObj.store.getUsedCapacity() > 0) {
                //
                // terminal
                if (creep.store[RESOURCE_ENERGY] == 0) {
                    // 掏空终端
                    // Terminal->Storage
                    creep.say('T->S')
                    for (const resourceType in terminalObj.store) {
                        creep.goTo(terminalObj.pos)
                        creep.withdraw(terminalObj, <ResourceConstant>resourceType)
                    }
                }
                // 填放资源
                if (storage) {
                    creep.memory.targetId = storage.id
                    creep.memory.working = true
                }
            }
            else {
                //
                // link
                if (creep.room.memory.centerLinkId) {
                    const centerLink = Game.getObjectById(creep.room.memory.centerLinkId)
                    const upgradeLink = Game.getObjectById(creep.room.memory.upgradeLinkId)

                    if (centerLink && upgradeLink) {
                        if (upgradeLink.store[RESOURCE_ENERGY] <= 100 && centerLink.store[RESOURCE_ENERGY] < UPGRADE_ENERGY_SEND_LIMIT) {
                            // Storage->Link
                            creep.say('S->L')
                            creep.goTo(storage.pos)
                            const status = creep.withdraw(storage, RESOURCE_ENERGY)
                            if (status == OK) {
                                creep.memory.targetId = centerLink.id
                                creep.memory.working = true
                            }
                        }
                        else if (centerLink.store[RESOURCE_ENERGY] > 0) {
                            // Link->Storage
                            creep.say('L->S')
                            creep.goTo(centerLink.pos)
                            const status = creep.withdraw(centerLink, RESOURCE_ENERGY)
                            if (status == OK) {
                                creep.memory.targetId = storage.id
                                creep.memory.working = true
                            }
                        }
                    }
                }
            }
        }

        if (creep.memory.working && creep.memory.targetId || creep.store.getFreeCapacity() == 0) {
            let targetId
            let target
            if(!creep.memory.targetId) {
                target = creep.room[STRUCTURE_STORAGE]
            }
            else {
                targetId = creep.memory.targetId
                target = Game.getObjectById(targetId) as StructureWithStore
            }

            if (target) {
                let status
                for (const resourceType in creep.store) {
                    creep.goTo(target.pos)
                    status = creep.transfer(target, <ResourceConstant>resourceType)
                }

                delete creep.memory.targetId
                creep.memory.working = false
            }
        }

        // RoleCenterManager.sleep(creep)
    }
}

export default RoleCenterManager
